package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.adapter.SearchAdapter;
import com.xca.znb.entity.Goods;
import com.xca.znb.model.impl.SearchImpl;
import com.xca.znb.presenter.SearchPresenter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class SearchActivity extends AppCompatActivity {

    private SearchPresenter presenter ;
    private List<Goods> goodsList ;
    private SearchAdapter adapter ;
    private EditText searchEditText ;
    private ImageButton searchButton  , searchBackButton;
    private ListView searchGoodsShow ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        searchButton = findViewById(R.id.search_btn) ;
        searchBackButton = findViewById(R.id.search_back_btn) ;
        searchEditText = findViewById(R.id.search_edit_text);
        searchGoodsShow = findViewById(R.id.search_goods_show);

        SearchImpl search = new SearchImpl(this) ;
        presenter = new SearchPresenter(search) ;

        searchBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputText = searchEditText.getText().toString().trim();
                if (inputText.isEmpty()){
                    Toast.makeText(SearchActivity.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
                } else {
                        goodsList = presenter.searchGoodsByName(inputText);
                        if (goodsList != null){
                            adapter = new SearchAdapter(SearchActivity.this , goodsList);
                            searchGoodsShow.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }else {
                            Toast.makeText(SearchActivity.this, "暂无人售卖", Toast.LENGTH_SHORT).show();
                        }

                }
            }
        });

        searchGoodsShow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView showId = view.findViewById(R.id.searchItem_goods_show_goodsId) ;
                String sId = showId.getText().toString();

                int goodsId = Integer.parseInt(sId);
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String phone = sharedPreferences.getString("phone" , "");
                LocalDateTime nowTime = LocalDateTime.now();
                String secTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

                Intent intent = new Intent(SearchActivity.this , GoodsDetailActivity.class) ;
                intent.putExtra("goodsId" , sId) ;
                presenter.addOrUpdateFoot(goodsId,phone,secTime);
                startActivity(intent);
            }
        });

        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                // 检查动作ID是否是搜索操作
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    // 获取搜索框内的文本
                    String query = v.getText().toString();

                    if (query.isEmpty()) {
                        // 如果搜索框为空，则显示提示
                        Toast.makeText(SearchActivity.this, "Please enter some text to search.", Toast.LENGTH_SHORT).show();
                        return true; // 消耗事件，不执行搜索
                    }else {
                        goodsList = presenter.searchGoodsByName(query);
                        if (goodsList != null){
                            adapter = new SearchAdapter(SearchActivity.this , goodsList);
                            searchGoodsShow.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }else {
                            Toast.makeText(SearchActivity.this, "暂无人售卖", Toast.LENGTH_SHORT).show();
                        }
                    }

                    // 处理搜索逻辑
                    performSearch(query);
                    return true; // 消耗事件
                }
                return false; // 没有消耗事件
            }
        });
    }


    private void performSearch(String query) {
        // 这里实现搜索逻辑，例如启动一个新的Activity或者更新当前界面的数据
        // 示例：启动一个新的搜索结果Activity
        Intent intent = new Intent(SearchActivity.this , LoginActivity.class);
        // intent.putExtra("search_query", query);
        startActivity(intent);
    }
}
